package com.yinhe.music.yhmusic.music;

import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.common.utils.JsonUtil;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusListObject;
import com.yinhe.music.event.RxbusNullObject;
import com.yinhe.music.type.person.BaseInfo;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.http.StatusCode;
import com.yinhe.music.yhmusic.network.response.Response;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;
import com.yinhe.music.yhmusic.share.SharePresenter;
import com.yinhe.music.yhmusic.utils.Preferences;

import io.reactivex.disposables.Disposable;

public class PlayingPresenter extends SharePresenter<BaseModel, PlayingContract.IPlayingView>
        implements PlayingContract.IPlayingPresenter {
    @Override
    public void setFav(int id, boolean isLike) {
        if (getView() == null) return;
        getView().showLoading();

        int[] idList = {id};

        if (isLike) {
            cancelFav(idList, isLike);
        } else {
            addToFav(idList, isLike);
        }
    }

    public void getRadioInfo(int id) {
        if (getView() == null) return;

        Disposable collectDisposable = mModel.getRadioInfo(id)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(radioListInfo -> {
                    getView().setRadioIntroUI(radioListInfo);
                }, throwable -> {
                    getView().showMessage(throwable);
                });
        mDisposable.add(collectDisposable);
    }

    private void addToFav(int[] idList, boolean isLike) {
        addSubscription(mModel.addSongToUserSongMenu(Preferences.getFavMenu(), JsonUtil.getJsonArray(idList))
                .compose(schedulerProvider.applySchedulers())
                .subscribe(baseInfoResponse -> {
                    handleResponse(baseInfoResponse, isLike);
                }, throwable -> {
                    getView().hideLoading();
                    getView().showMessage(throwable);
                }));
    }

    private void cancelFav(int[] idList, boolean isLike) {
        addSubscription(mModel.deleteUserSongMenuSong(Preferences.getFavMenu(), JsonUtil.getJsonArray(idList))
                .compose(schedulerProvider.applySchedulers())
                .subscribe(baseInfo -> {
                            handleResponse(baseInfo, isLike);
                        },
                        throwable -> {
                            getView().showMessage(throwable);
                        }));
    }

    private void handleResponse(Response<BaseInfo> baseInfo, boolean isLike) {
        if (baseInfo.getCode() == StatusCode.CHANGE_DATA) {
            getView().updateFavStatus();
            if (isLike) {
                RxBus.get().post(RxBusEventType.SongMenu.DELETE_MENU_MUSIC, RxbusListObject.instance(null));
            } else {
                RxBus.get().post(RxBusEventType.SongMenu.ADD_MUSIC_TO_MENU, RxbusNullObject.INSTANCE);
            }
        }
        getView().showToast(baseInfo.getMsg());
        getView().hideLoading();
    }
}
